import hashlib
import datetime
import torque.settings as settings
from django.db import models
from django.contrib.auth.models import User
from django.core.mail import EmailMessage


class Invite(models.Model):
    code = models.CharField("User Invite",max_length=255)
    inviter = models.ForeignKey(User)
    email = models.EmailField("Email",max_length=255)
    creation = models.DateTimeField(auto_now_add=True)
    accepted = models.DateTimeField(blank=True,null=True)
    declined = models.DateTimeField(blank=True,null=True)
    sent = models.BooleanField(default=False)
    enabled = models.BooleanField(default=True)
    
    def __unicode__(self):
        return self.email

    def send_invite(self):
        now = datetime.datetime.now()
	self.code = hashlib.sha1(str(now)).hexdigest()
	subject = '%s Invitation' % settings.TORQUE['name']
        from_email = settings.TORQUE['email']
	to_email = [str(self.email)]
	html_content = '<div align="center"><img src="%sresources/images/logo1light.jpg"/></div><p>You have been invited to %s by %s.</p><p>You can register <a href="%sinvite/%s/">here</a>.</p>' % (settings.TORQUE['url'],settings.TORQUE['name'], self.inviter, settings.TORQUE['url'], self.code)
	msg = EmailMessage(subject, html_content, from_email, to_email)
	msg.content_subtype="html"
	msg.send()
